<?php

namespace app\model;

use app\BaseModel;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;
use app\model\not_del\UserModel as NotDelUserModel;

/**
 * 量表
 */
class RatingScaleModel extends BaseModel
{
    protected $name = 'rating_scale';

    /**
     * 新增数据验证
     * @return array
     */
    public function addValidate(): array
    {
        return [
            'unit_id|单位ID'  => 'require',
            'type_id|量表种类'  => 'require',
            'name|量表名'  => 'require',
        ];
    }

    /**
     * 关联量表类型表-类型名称
     * @return BelongsTo
     */
    public function ratingScaleTypeName(): BelongsTo
    {
        return $this->belongsTo(RatingScaleTypeModel::class, 'type_id')->bind(['rating_scale_type_name' => 'name']);
    }

    /**
     * 关联量表模板
     * @return BelongsTo
     */
    public function RatingScaleMode(): BelongsTo
    {
        return $this->belongsTo(RatingScaleModeModel::class, 'mode_id')->bind(['mode_name' => 'name']);
    }

    /**
     * 量表题目
     * @return HasMany
     */
    public function RatingScaleQuestion(): HasMany
    {
        return $this->hasMany(RatingScaleQuestionModel::class, 'rating_scale_id')->bind([
            'question_num' => 'num',
            'question_name' => 'name'
        ]);
    }

    /**
     * 用户
     * @return BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(NotDelUserModel::class, 'uid')->bind([
            'user_name' => 'name'
        ]);
    }

    /**
     * 因子
     * @return HasMany
     */
    public function factor(): HasMany
    {
        return $this->hasMany(RatingScaleFactorModel::class, 'rating_scale_id')->field('id,rating_scale_id,mode_id,name');
    }
}